package com.yj.timecapsule.common.handler;


import com.yj.timecapsule.common.enums.ErrorCode;
import com.yj.timecapsule.common.error.BusinessException;
import com.yj.timecapsule.common.response.CommonResponseType;
import lombok.extern.slf4j.Slf4j;;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Sean
 * exceptionInterceptor
 */
@RestControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    @ExceptionHandler(Exception.class)
    public CommonResponseType<Object> handler(Exception e){
        if (e instanceof BusinessException){
            BusinessException be = (BusinessException) e;

            ErrorCode errorCode = be.getErrorCode();
            log.error("业务异常:{}",errorCode.getMsg(),be);
            return CommonResponseType.error(errorCode.getCode(), errorCode.getMsg());
        }else{
            log.error("系统异常:{}",e.getMessage(),e);
            return CommonResponseType.error(ErrorCode.SERVER_ERROR.getCode(),ErrorCode.SERVER_ERROR.getMsg());
        }
    }


}
